package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellercs.parameter.PcOffLineBean;
import com.ymatou.iapi.sellercs.service.PcOffLineCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtOnlinecs;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;
/**
*Pc下线
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_PcOffLine{
	private static PcOffLineBean pcofflineBean;
	private static PcOffLineCall pcofflineCall;
	YmtSellerCSIMWapper YmtSellerCSIMWapper;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("Pc下线");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		pcofflineBean=new PcOffLineBean();
		pcofflineCall=new PcOffLineCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		SellerCSIMCallService.OffLine(sellerId, null);
		SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(pcofflineCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PcOffLine_001() {
		Logger.start(true,"新数据Pc下线成功,LoginStatus=1删除记录");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			String SocketIoToken=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(mcsid, csid, SocketIoToken);
			
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
			//TODO OffLineType必填但是无用 枚举但是可以随便传
			pcofflineBean.setOffLineType(1);
			pcofflineBean.setSocketIoToken(SocketIoToken);
			pcofflineCall.setData(pcofflineBean);
			pcofflineCall.callService();			
			
			Logger.verifyIsContains("true", pcofflineCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", pcofflineCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			Logger.comment("验证日志包含发送指令op=0登出");
			List<String> filter2 = loglist.stream().filter(s->s.contains("op=0")).collect(Collectors.toList());
			Logger.debug(filter2);
			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			
			List<YmtOnlinecs> info = YmtSellerCSIMWapper.selectYmtOnlinecs(null, csid);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(0, info.size(), "验证记录删除");
			//TODO Redis移除会话列表、移除会话详情。
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcOffLine_002() {
		Logger.start(true,"不输入OffLineType");
		//默认0 无枚举
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			String SocketIoToken=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(mcsid, csid, SocketIoToken);
			
			//TODO OffLineType必填但是无用 枚举但是可以随便传
//			pcofflineBean.setOffLineType(1);
			pcofflineBean.setSocketIoToken(SocketIoToken);
			pcofflineCall.setData(pcofflineBean);
			pcofflineCall.callService();	
			Logger.verifyIsContains("true", pcofflineCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", pcofflineCall.getString("Status"), "验证Status");
			List<YmtOnlinecs> info = YmtSellerCSIMWapper.selectYmtOnlinecs(null, csid);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(0, info.size(), "验证记录删除");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcOffLine_003() {
		Logger.start(true,"不输入SocketIoToken失败");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			String SocketIoToken=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(mcsid, csid, SocketIoToken);
			//TODO OffLineType必填但是无用 枚举但是可以随便传
//			pcofflineBean.setOffLineType(1);
//			pcofflineBean.setSocketIoToken(SocketIoToken);
			pcofflineCall.setData(pcofflineBean);
			pcofflineCall.callService();	
			Logger.verifyIsContains("400", pcofflineCall.getString("Status"), "验证Status");
			List<YmtOnlinecs> info = YmtSellerCSIMWapper.selectYmtOnlinecs(null, csid);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(1, info.size(), "验证记录未删除");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcOffLine_004() {
		Logger.start(true,"Pc下线成功,LoginStatus=2不变");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			String SocketIoToken=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(mcsid, csid, SocketIoToken);
			YmtSellerCSIMWapper.updateYmtOnlinecsSetLoginStatus(csid, 2);
			
			pcofflineBean.setOffLineType(1);
			pcofflineBean.setSocketIoToken(SocketIoToken);
			pcofflineCall.setData(pcofflineBean);
			pcofflineCall.callService();			
			
			Logger.verifyIsContains("true", pcofflineCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", pcofflineCall.getString("Status"), "验证Status");
			
			List<YmtOnlinecs> infos = YmtSellerCSIMWapper.selectYmtOnlinecs(null, csid);
			Logger.verifyEquals(1, infos.size(), "验证记录未删除");
			Logger.debug(FormatUtil.beanToGSONString(infos));
			YmtOnlinecs info = infos.get(0);
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			
			Logger.verifyEquals(2, info.getLoginStatus(), "验证loginStatus为在线2");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PcOffLine_005() {
		Logger.start(true,"Pc下线成功,LoginStatus=3变2");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			String SocketIoToken=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(mcsid, csid, SocketIoToken);
			YmtSellerCSIMWapper.updateYmtOnlinecsSetLoginStatus(csid, 3);
			
			pcofflineBean.setOffLineType(1);
			pcofflineBean.setSocketIoToken(SocketIoToken);
			pcofflineCall.setData(pcofflineBean);
			pcofflineCall.callService();			
			
			Logger.verifyIsContains("true", pcofflineCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", pcofflineCall.getString("Status"), "验证Status");
			
			List<YmtOnlinecs> infos = YmtSellerCSIMWapper.selectYmtOnlinecs(null, csid);
			Logger.verifyEquals(1, infos.size(), "验证记录未删除");
			Logger.debug(FormatUtil.beanToGSONString(infos));
			YmtOnlinecs info = infos.get(0);
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			
			Logger.verifyEquals(2, info.getLoginStatus(), "验证loginStatus为在线2");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}